window.CH = {};
CH.utils = {};
CH.common = {};
/***
 * 创建包名(java中为包, c#中为命名空间)
 * @param {} package_name, for example: com.usercard.controller
 */
CH.package = function(package_name) {
    var firstNode = "";
    var parentNodes = [];
    var nodes = package_name.split(".");
    for(var i = 0; i < nodes.length; i++) {
        var currentNode = nodes[i];

        if(i == 0)
        	firstNode = currentNode;

        if(i > 0) 
        	parentNodes.push(currentNode);
    }
    
    if(!window[firstNode]) {
     	window[firstNode] = {};
    }
    
    var thePackage = firstPackage = window[firstNode];
    for(var i = 0; i < parentNodes.length; i++) {
        var nodeName = parentNodes[i];

        for(var n = 0; n < i; n++) {
            var pNode = parentNodes[n];
            if(!thePackage) {
                thePackage = firstPackage[pNode];
            } else {
                thePackage = thePackage[pNode];
            }
        }

        if(!thePackage[nodeName]) {
            thePackage[nodeName] = {};
        }
    }
}

/***
 * 创建一个类, 面向对象中的类:Class
 * @param {} config: 创建类时定义的成员变量和方法
 * @return {}
 */
CH.create = function(config) {
    var func = function(obj) {
    	if(this.construct) {
    		this.construct.call(this, obj);
    	}	
    };
    
    if(!config) return func;
    
    for (var key in config) {
        func.prototype[key] = config[key];
    }
    
    return func;
}

/***
 * 继承一个父类，并扩展父类的方法和成员变量
 * @param {} func
 * @param {} config
 * @return {}
 */
CH.extends = function(func, config) {
    for (var key in config) {
        func.prototype[key] = config[key];
    }

    return func;
}

/***
 * 封装ajax请求的get请求
 * @param {} url
 * @param {} data
 * @param {} success
 */
CH.GetRequest = function(url, data, success, _this_) {
	var args = arguments;
	if(args.length == 3) {
		_this_ = success;
		success = data;
		data = undefined;
	}
	
	if(args.length == 2) {
		success = data;
		data = undefined;
	}
	
	(function($) {
		if(data) {
			$.get(url, data, function(result) {
				success.call(_this_, result);
			});
		} else {
			$.get(url, function(result) {
				success.call(_this_, result);
			});
		}
		
	})(jQuery);
}

/***
 * 封装ajax请求的post请求
 * @param {} url
 * @param {} data
 * @param {} success
 */
CH.PostRequest = function(url, data, contentType, success, _this_) {
	var args = arguments;
	(function($) {
		if(args.length == 4) {
			_this_ = success;
			success = contentType;
			contentType = undefined;
		}
		
		if(args.length == 3) {
			_this_ = null;
			success = contentType;
			contentType = undefined;
		}
		
		var config = {
		  	type: "POST",
		  	url: url,
		  	data: data,
		  	success: function(result) {
				success.call(_this_, result);
		  	}
		};
		
		if(contentType) {
			config.contentType = contentType;
		}
		
		$.ajax(config);
		
	})(jQuery);
}

/***
 * 封装ajax请求的put请求
 * @param {} url
 * @param {} data
 * @param {} success
 */
CH.PutRequest = function(url, data, success, _this_) {
	(function($) {
		$.ajax({
			url: url,
			type: 'PUT',
			data: data,
			success: function(result){
				success.call(_this_, result);
			}
		}); 
	})(jQuery);
}

/***
 * 封装ajax请求的delete请求
 * @param {} url
 * @param {} data
 * @param {} success
 */
CH.DeleteRequest = function(url, success, _this_) {
	(function($) {
		$.ajax({
			url: url,
			type: 'DELETE',
			beforeSend: function(xhr) {  
			    xhr.setRequestHeader("Accept", "application/json");  
			    xhr.setRequestHeader("Content-Type", "application/json");  
			},
			success: function(result){
				success.call(_this_, result);
			}
		});
	})(jQuery);
}

CH.JQ = (function($) {
		return $;
})(jQuery);

/***
 * 直接集成jtemplate 模板
 * @param {} renderTo, 显示内容的容器ID
 * @param {} tplId, 模板ID
 * @param {} data，数据
 */
CH.template = function(renderTo, templateId, data) {
	(function($) {
		//设置模板
		$(renderTo).setTemplateElement(templateId);  
        // 处理模板  
        $(renderTo).processTemplate(data);  
        
        var html = $('#'+templateId).html().trim();
		if(!html.startWith('<!--')) {
			html = '<!--' + html + '-->';
			$('#'+templateId).html(html);
		}
	})(jQuery);
}

CH.utils.showMessageOK = function(message) {
	$.scojs_message(message, $.scojs_message.TYPE_OK);
}

CH.utils.showMessageError = function(message) {
	$.scojs_message(message, $.scojs_message.TYPE_ERROR);
}

CH.utils.toLocalTime = function(t) {     
	return new Date(parseInt(t)).Format('yyyy-MM-dd hh:mm');
} 

/*CH.template = function(selector, data, func) {
	(function($) {
		var template_el = $(selector);
		var mode = template_el.attr('mode');
		if(mode == 'single') {
			for(var field in data) {
				var query_express = "input[data-bind='"+field+"']";
				var el = template_el.find(query_express);
				var value = data[field];
				var type = el.attr('type');
				
				if(func) value = func(field, value);
				
				if(type == 'checkbox') {
					if(value == 'true' || value == 1 || value == '1') {
						el.attr('checked', 'true');
					} else {
						el.removeAttr("checked");
					}
				} else {
					el.val(value);
				}
			}
		}
	})(jQuery);
}*/







